package san.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import com.security.domain.Resource;
import com.security.domain.Role;
import com.security.domain.User;
import com.security.resource.SecurityResourceCache;
import com.security.service.UserService;

@SuppressWarnings("unused")
public class InitSystemData {
	@Autowired
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	private boolean isRun;

	public void init() throws Exception {
		if (isRun) {// 每次启动内存库为true
			initResource();
			initRole();
			initUser();
		} else {// 如果是普通数据库，需要初始化资源缓存
			initCache();
		}
	}

	public void deleteAll() throws Exception {
		userService.deleteAllResource();
		userService.deleteAllRole();
		userService.deleteAllUser();
		//初始化
		initResource();
		initRole();
		initUser();	
	}

	public void initCache() {
		List<Resource> res = userService.getAllResources();
		for (Resource r : res) {
			SecurityResourceCache.putCache(r);
		}
	}

	public void initUser() throws Exception {
		List<Role> roles = new ArrayList<Role>();
		roles = userService.getAllRoles();
		User user = new User();
		// =====1===============
		user.setUsername("admin");
		user.setCnname("超级管理员");
		user.setEmail("994155125@qq.com");
		user.setPassword("admin");
		user.setRoles(roles);
		userService.saveUser(user);
		// =====2===============
		user = new User();
		user.setUsername("user");
		user.setCnname("用户");
		user.setEmail("994155125@qq.com");
		user.setPassword("user");
		roles.clear();
		roles = userService.getAllRoles();
		user.setRoles(roles);
		userService.saveUser(user);
	}

	public void initRole() throws Exception {
		List<Resource> aus = new ArrayList<Resource>();
		Role role = new Role();
		role.setName("ADMIN");
		role.setCnname("管理员 ");
		aus = userService.getAllResources();
		Resource au = new Resource();
		role.setResources(aus);
		userService.saveRole(role);
		// ======1==============
		role = new Role();
		role.setName("USER");
		role.setCnname("普通用户");
		aus.clear();
		aus = userService.getAllResources();
		role.setResources(aus);
		userService.saveRole(role);
		// =====2==============
	}

	public void initResource() throws Exception {
		Resource r = new Resource();
		r.setPath("/system/user/save*.shtml");
		r.setName("RES_USER_SAVE");
		r.setCnname("保存用户");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/system/user/delete*.shtml");
		r.setName("RES_USER_REMOVE");
		r.setCnname("删除用户");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/system/user*.shtml");
		r.setName("RES_USER_VIEW");
		r.setCnname("查看用户");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/system/role/save*.shtml");
		r.setName("RES_ROLE_SAVE");
		r.setCnname("保存角色");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/system/role/delete*.shtml");
		r.setName("RES_ROLE_REMOVE");
		r.setCnname("删除角色");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/system/role*.shtml");
		r.setName("RES_ROLE_VIEW");
		r.setCnname("查看角色");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/system/resource*.shtml");
		r.setName("RES_RESOURCE_VIEW");
		r.setCnname("查看资源");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/system/resource/save*.shtml");
		r.setName("RES_RESOURCE_SAVE");
		r.setCnname("保存资源");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/system/resource/delete*.shtml");
		r.setName("RES_RESOURCE_REMOVE");
		r.setCnname("删除资源");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/article/article*");
		r.setName("RES_ARTICLE_VIEW");
		r.setCnname("查看文章类型");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/article/articleitem*");
		r.setName("RES_ARTICLEITEM_VIEW");
		r.setCnname("查看文章");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/article/article/input*");
		r.setName("RES_ARTICLE_INPUT");
		r.setCnname("添加文章类型");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/article/article/save*");
		r.setName("RES_ARTICLE_SAVE");
		r.setCnname("保存文章类型");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/article/article/search*");
		r.setName("RES_ARTICLE_SEARCH");
		r.setCnname("全文检索");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/article/articleitem/input*");
		r.setName("RES_ARTICLEITEM_INPUT");
		r.setCnname("添加文章");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/article/articleitem/save*");
		r.setName("RES_ARTICLEITEM_SAVE");
		r.setCnname("保存文章");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/article/articleitem/delete*");
		r.setName("RES_ARTICLEITEM_DELETE");
		r.setCnname("删除文章类型");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/article/article/delete*");
		r.setName("RES_ARTICLE_DELETE");
		r.setCnname("删除文章");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/dictionary/dictionary*");
		r.setName("RES_DICTIONARY_ALL");
		r.setCnname("操作字典表");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/demo/up-or-down/delete*");
		r.setName("RES_FILE_ALL");
		r.setCnname("删除文件");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/demo/up-or-down/input*");
		r.setName("RES_FILE_INPUT");
		r.setCnname("上传文件");
		userService.saveResource(r);
		// =================================
		r = new Resource();
		r.setPath("/demo/**");
		r.setName("RES_DEMO_ALL");
		r.setCnname("演示功能");
		userService.saveResource(r);
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public boolean isRun() {
		return isRun;
	}

	public void setRun(boolean isRun) {
		this.isRun = isRun;
	}

}
